 /**    
  *******************************************************************************************************    
  * @attention    
  * Copyright (c) 2023, SHANGHAI FUDAN MICROELECTRONICS GROUP CO., LTD.(FUDAN MICROELECTRONICS./ FUDAN MICRO.)    
  * All rights reserved.    
  *    
  * Processor:                   FM33FG0xxA    
  * http:                        http://www.fmdevelopers.com.cn/    
  *    
  * Redistribution and use in source and binary forms, with or without    
  * modification, are permitted provided that the following conditions are met    
  *    
  * 1. Redistributions of source code must retain the above copyright notice,    
  *    this list of conditions and the following disclaimer.    
  *    
  * 2. Redistributions in binary form must reproduce the above copyright notice,    
  *    this list of conditions and the following disclaimer in the documentation    
  *    and/or other materials provided with the distribution.    
  *    
  * 3. Neither the name of the copyright holder nor the names of its contributors    
  *    may be used to endorse or promote products derived from this software    
  *    without specific prior written permission.    
  *    
  * 4. To provide the most up-to-date information, the revision of our documents     
  *    on the World Wide Web will be the most Current. Your printed copy may be      
  *    an earlier revision. To verify you have the latest information avaliable,    
  *    refer to: http://www.fmdevelopers.com.cn/.    
  *    
  * THIS SOFTWARE IS PROVIDED BY FUDAN MICRO "AS IS" AND ANY EXPRESSED:     
  * ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES     
  * OF MERCHANTABILITY NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE    
  * ARE DISCLAIMED.IN NO EVENT SHALL FUDAN MICRO OR ITS CONTRIBUTORS BE LIABLE     
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL     
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS     
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER    
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,     
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE     
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OFTHE POSSIBILITY OF SUCH DAMAGE.   
  *
  ******************************************************************************************************* 
  */    

#include "gptim.h"
#include "pll.h"

/* 1ms 周期标志 */
volatile uint8_t Period10msFlag = 0;


/**
* @brief  GPTIM中断服务函数
  * @param  void
  * @retval void
  */
void GPTIMX_IRQHandler(void)
{
    if((FL_GPTIM_IsEnabledIT_Update(GPTIM1)) \
      &&FL_GPTIM_IsActiveFlag_Update(GPTIM1))
    {
        FL_GPTIM_ClearFlag_Update(GPTIM1);
        Period10msFlag = 1;
    }
}

/**
  * @brief  GPTIM初始化
  * @param  void
  * @retval void
  */
void GPTIM_Init(void)
{
    FL_GPTIM_InitTypeDef      TimerBase_InitStruct;
    FL_NVIC_ConfigTypeDef     InterruptConfigStruct;
  
    /* 定时器频率计算：系统主频/(分频系数*自动重装载值) */
    TimerBase_InitStruct.prescaler       = 1000;                       /* 分频系数配置：8000 */
    TimerBase_InitStruct.counterMode     = FL_GPTIM_COUNTER_DIR_UP;      /* 计数模式配置：向上计数 */
    TimerBase_InitStruct.autoReload      = 32 - 1;                       /* 自动重装载值配置：1000 */
    TimerBase_InitStruct.clockDivision   = FL_GPTIM_CLK_DIVISION_DIV1;   /* 死区和滤波分频配置：1分频 */
    TimerBase_InitStruct.autoReloadState = FL_ENABLE;                    /* 预装载配置：使能 */
    FL_GPTIM_Init(GPTIM1, &TimerBase_InitStruct);
  
    /* 清除Update标志 */
    FL_GPTIM_ClearFlag_Update(GPTIM1);
    /* 使能Update中断 */
    FL_GPTIM_EnableIT_Update(GPTIM1);

    /* 使能GPTIM模块 */
    FL_GPTIM_Enable(GPTIM1);

    /* 配置NVIC */
    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, GPTIMX_IRQn);
}
